package com.linchaoen.elema.service.impl;

import com.linchaoen.elema.config.CacheKeyManager;
import com.linchaoen.elema.mapper.LabelMapper;
import com.linchaoen.elema.model.entity.Label;
import com.linchaoen.elema.model.entity.Store;
import com.linchaoen.elema.model.entity.StoreLabel;
import com.linchaoen.elema.service.LabelService;
import com.linchaoen.elema.utils.BaseCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LabelServiceImpl implements LabelService {

    @Autowired
    private LabelMapper labelMapper;

    @Autowired
    private BaseCache baseCache;

    @Override
    public List<Label> queryLabelBySid(Integer storeId) {

        try {
            String key = String.format(CacheKeyManager.INDEX_LABEL_KEY, storeId);
            Object cacheObj = baseCache.getTenMinuteCache().get(key, () -> {
                List<Label> labelList = labelMapper.queryLabelBySid(storeId);
                return labelList;
            });
            if (cacheObj instanceof List) {
                List<Label> labelList = (List<Label>) cacheObj;
                return labelList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public List<StoreLabel> getStoreByLabel() {
        return labelMapper.findStoreByLabel();
    }


}
